<?php
// +----------------------------------------------------------------------
// | OneKeyAdmin [ Believe that you can do better ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://onekeyadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: MUKE <513038996@qq.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

use app\index\model\Config;
use plugins\blog\index\model\Blog;
use plugins\blog\index\model\BlogComment;

/**
 * 指定博客列表
 */
function blog(array $type = [], int $limit = 10, int $catalog_id = 0, array $order = ['sort' => 'desc', 'create_time' => 'desc'], $user_id = 0): object
{
    $where[] = ['status', '=', 1]; 
    foreach ($type as $k => $v) {
        $where[] = [$v, '=', 1];
    }
    if (! empty($user_id)) {
        $where[] = ['user_id', '=', $user_id];
    }
    if (! empty($catalog_id)) {
        $where[] = ["catalog_id", "find in set", $catalog_id];
    }
    return Blog::where($where)->order($order)->limit($limit)->append(['url,content_images','comment_count','collect_count','catalog_title'])->select();
}

/**
 * 热门博主
 */
function blog_user(int $limit = 10, int $catalog_id = 0): object
{
    $where = [];
    if (! empty($catalog_id)) {
        $where[] = ["catalog_id", "find in set", $catalog_id];
    }
    return Blog::field('user_id,count(user_id) as publish_count')->where('user_id', '<>', 0)->with(['user'])->order('publish_count','desc')->group('user_id')->select();
}

/**
 * 博客配置
 */
function blog_config() 
{
    return Config::getVal('app_blog');
}

/**
 * 博客数量
 */
function blog_count() 
{
    return Blog::count();
}

/**
 * 评论数量
 */
function blog_comment_count() 
{
    return BlogComment::count();
}